﻿using System.Collections.Generic;
using Quiziqal.Application.Services;
using SharpArch.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Data.NHibernate;
using SharpArch.Testing.NUnit;
using SharpArch.Testing.NUnit.NHibernate;

using NUnit.Framework;
using Rhino.Mocks;

using Quiziqal.Application;
using Quiziqal.Domain;
using Quiziqal.Persistence;
using Quiziqal.Specifications.Persistence;

namespace Quiziqal.Specifications.Core.Services
{
    [TestFixture]
    public class when_getting_quizzes_for_user : PersistenceTestBase
    {
        ICollection<Quiz> quizzes = new List<Quiz>();
        IQuizRepository repository;
        QuiziqalService service;
        User owner;

        protected override void EstablishContext()
        {
            repository = new QuizRepository();
            service = new QuiziqalService(repository);

            owner = CreatePersistedUser("arthur_dent");
            CreatePersistedQuiz("Quiz C", owner);
            CreatePersistedQuiz("Quiz A", owner);
            CreatePersistedQuiz("Quiz B", owner);
        }

        protected override void Act()
        {
            quizzes = service.GetQuizzesByOwner(owner);
        }

        [Test]
        public void count_should_be_correct()
        {
            quizzes.Count.ShouldEqual(3);
        }

        [Test]
        public void should_be_ordered_by_name_ascending()
        {
            Quiz previousQuiz = null;

            foreach(Quiz currentQuiz in quizzes)
            {
                if (previousQuiz == null) previousQuiz = currentQuiz;
                currentQuiz.Name.ShouldBeGreaterThanOrEqualTo(previousQuiz.Name);
                previousQuiz = currentQuiz;
            }
        }
    }
}
